package love.simbot.example.listen;

import catcode.CatCodeUtil;
import love.forte.simbot.annotation.Listener;
import love.forte.simbot.annotation.OnGroup;
import love.forte.simbot.annotation.OnGroupAddRequest;
import love.forte.simbot.api.message.MessageContent;
import love.forte.simbot.api.message.containers.AccountInfo;
import love.forte.simbot.api.message.containers.GroupInfo;
import love.forte.simbot.api.message.events.GroupAddRequest;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.message.events.MessageGet;
import love.forte.simbot.api.message.events.PrivateMsg;
import love.forte.simbot.api.sender.Getter;
import love.forte.simbot.api.sender.Sender;
import love.forte.simbot.api.sender.Setter;
import love.simbot.example.constant.Constant;
import love.simbot.example.listen.listenServer.GroupService;
import love.simbot.example.listen.listenServer.ListenerService;

import java.util.Arrays;
import java.util.Set;

@Listener
public class GroupListener {


    // Getter 用于获取群成员的信息
    @OnGroup
    public void groupLinstener(GroupMsg groupMsg,
                               MessageGet getter,
                               Getter geter,
                               Setter setter,
                               Sender sender){



        MessageContent msgContent = groupMsg.getMsgContent();
        System.out.println("群聊使用者:"+getter.getAccountInfo().getAccountCodeNumber()+"\t"+"输入的内容:"+msgContent.getMsg());



        // 获取菜单处理
        String message = msgContent.getMsg();
        // 敏感词检查
        GroupService.badCheck(message,groupMsg,setter,getter,sender);


        long account = getter.getAccountInfo().getAccountCodeNumber();
        String code = GroupService.codeMap.get(account);

        String groupCode = GroupService.codeMap.get(groupMsg.getGroupInfo().getGroupCodeNumber());

        // 是否是聊天模式,以及是否关闭
        if(code != null && !("exit".equals(message)) && "D".equals(code)){
            GroupService.chatMessageGet(sender,groupMsg,getter);
        }
        else if(code != null && "exit".equals(message) && "D".equals(code)){
            GroupService.codeMap.remove(account);
            sender.sendGroupMsg(groupMsg,"已退出聊天模式");
        }

        // 上次的指令对下次有影响的
        if(code != null){
            nextMethod(sender,code,groupMsg,getter,setter);
            return;
        }

        if(groupCode != null){
            nextMethod(sender,groupCode,groupMsg,getter,setter);
            return;
        }

        // 有定时任务需要取消的
        if(GroupService.timeTaskMap.get(account) != null && "cancel".equals(message)){
            GroupService.cancelTimeTask(account,groupMsg,sender);
        }


        if(message.equals("菜单")){
            // 1.获取私有菜单
            getGroupMenu(groupMsg,sender);
        }
        // 执行菜单的中的某些方法
        if(message.matches("^[\\w] [\\s\\S]*")){
            invokeMenuMethod(message,groupMsg,geter,setter,getter,sender);
        }
        return;
    }

    // 群申请监听
    @OnGroupAddRequest
    public void groupAddRequestListener(GroupAddRequest request,
                                        Setter setter,
                                        Sender sender) {
        // 1.获取到群添加者的申请信息
        AccountInfo accountInfo = request.getAccountInfo();
        // 2.获取申请的群信息
        GroupInfo groupInfo = request.getGroupInfo();

        String text = accountInfo.getAccountNickname()
                +"("+accountInfo.getAccountCodeNumber()+")\n"
                +"以【"+request.getText()+"】为理由\n"
                +"申请加群,管理员如果同意请输入【同意】/ 不同意则输入【不同意】 ";


        sender.sendGroupMsg(groupInfo.getGroupCodeNumber(),text);
        GroupService.codeMap.put(groupInfo.getGroupCodeNumber(),"H");
        GroupService.groupAddRequestMap.put(request.getGroupInfo().getGroupCodeNumber(),request);
    }






    // 执行菜单的方法
    private void invokeMenuMethod(String myMessage,
                                  GroupMsg groupMsg,
                                  Getter geter,
                                  Setter setter,
                                  MessageGet getter,
                                  Sender sender) {

        //1.将用户输入的指令进行拆解
        char instruction = myMessage.charAt(0);


        Set<Character> menus = GroupService.closeMenu.get(groupMsg.getGroupInfo().getGroupCode());
        if(menus != null && menus.contains(instruction)){
            sender.sendGroupMsg(groupMsg,"此群的该功能已关闭");
            return;
        }


        switch (instruction){
            case 'A':
                sender.sendGroupMsg(groupMsg, Constant.GROUP_TIP);
                break;
            case 'B':
                GroupService.searchMusic(myMessage,sender,groupMsg,getter);
                // 歌曲搜索
                break;
            case 'C':
                // 语言翻译
                GroupService.translationLanguage(myMessage,groupMsg,getter,sender);
                break;
            case 'D':
                GroupService.chatModeStart(sender,groupMsg,getter);
                break;
            case 'E':
                // 定时提醒
                sender.sendGroupMsg(groupMsg,"对不起,在群里面这个功能是禁用的");
              /*  GroupService.regularlyRemind(getter,groupMsg,sender);*/
                break;
            case 'G':
                // 疫情查询
                GroupService.epidmicGet(sender,groupMsg,getter);
                break;
            case 'H':
                GroupService.groupManage(sender,geter,setter,groupMsg,getter);
                break;
            case 'I':
               /* GroupService.searchBiliBiliVideo(sender,groupMsg,getter);*/
                break;
            case 'J':
                GroupService.sendImage(groupMsg,sender);
                break;
        }

    }

    // 获取群聊菜单
    private  void getGroupMenu(GroupMsg groupMsg,Sender sender){
        sender.sendGroupMsg(groupMsg, Constant.GROUP_MENU);
    }



    private void nextMethod(Sender sender,String code,GroupMsg msg,
                            MessageGet messageGet,Setter setter){
        char index = code.charAt(0);
        switch (index){
            case 'B':
                GroupService.shareMusic(sender,msg,messageGet);
                break;
            case 'I':
               /* GroupService.shareBiliBiliVideo(sender,msg,messageGet);*/
                break;
            case 'H':
                GroupService.agreeOrdisagreeGroupRequest(
                        GroupService.groupAddRequestMap.get(msg.getGroupInfo().getGroupCodeNumber()),
                        msg,
                        messageGet,
                        setter,sender);
                break;
        }
    }

}
